/**
 * Copyright 2010 upTick Pty Ltd
 *
 * Licensed under the terms of the GNU General Public License version 3 
 * as published by the Free Software Foundation. You may obtain a copy of the
 * License at: http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations 
 * under the License.
 */

package au.com.uptick.serendipity.server.domain.dao;

import au.com.uptick.serendipity.server.domain.Account;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.TypedQuery;
import java.util.List;

public class AccountDao {

    public static final EntityManager entityManager() {
        return Emf.get().createEntityManager();
    }

    public void createAccount(Account account) {

        EntityManager em = entityManager();
        EntityTransaction tx = em.getTransaction();

        try {
            tx.begin();
            em.persist(account);
            tx.commit();
        } catch (Throwable t) {
            t.printStackTrace();
            tx.rollback();
        } finally {
            em.close();
        }
    }

    public Account retrieveAccount(Long accountId) {

        EntityManager em = entityManager();
        Account account = null;

        try {
            TypedQuery<Account> query = em.createQuery("select a from Account a where a.accountId = ?1", Account.class);
            query.setParameter(1, accountId);
            account = query.getSingleResult();
        } finally {
            em.close();
        }

        return account;
    }

    public List<Account> retrieveAccounts() {

        EntityManager em = entityManager();
        List<Account> list = null;

        try {
            TypedQuery<Account> query = em.createQuery("select a from Account a", Account.class);
            list = query.getResultList();
        } finally {
            em.close();
        }

        return list;
    }

    public Account updateAccount(Account account) {

        EntityManager em = entityManager();
        EntityTransaction tx = em.getTransaction();
        Account account2 = null;

        try {
            tx.begin();
            account2 = em.merge(account);
            tx.commit();
        } catch (Throwable t) {
            t.printStackTrace();
            tx.rollback();
        } finally {
            em.close();
        }

        return account2;
    }

    public void deleteAccount(Account account) {

        EntityManager em = entityManager();
        EntityTransaction tx = em.getTransaction();

        try {
            tx.begin();
            em.remove(em.merge(account));
            tx.commit();
        } catch (Throwable t) {
            t.printStackTrace();
            tx.rollback();
        } finally {
            em.close();
        }
    }
}
